package com.think.teaching.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.think.teaching.model.Course;
import com.think.teaching.model.Tag;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 Mapper 接口
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
public interface CourseMapper extends BaseMapper<Course> {

    @Select("<script>" +
            "SELECT * FROM course WHERE delflag=0 " +
            "<if test='type gt 0'>AND type=#{type}</if>"
            +
            "<if test='state gt 0'>AND state =#{state}</if>"
            +
            "<if test='mode gt 0'>AND mode = #{mode}</if>"
            +
            "AND template=#{tempFlag}"
            +
            "</script>"
    )
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "mode", property = "mode"),
            @Result(column = "course_img", property = "courseImg"),
            @Result(column = "updated_time", property = "updatedTime"),
            @Result(column = "score", property = "score"),
            @Result(column = "state", property = "state"),
            @Result(column = "id", property = "tags",
                    one = @One(select = "com.think.teaching.mapper.CourseMapper.getCourseTagById", fetchType = FetchType.EAGER)),
            @Result(column = "type", property = "typeInfo",
                    one = @One(select = "com.think.teaching.mapper.CourseTypeMapper.getInfoById", fetchType = FetchType.EAGER))
    })
    IPage<Course> getCourseListWithType(IPage<Course> page,
                                        @Param("tempFlag") Integer tempFlag,
                                        @Param("state") Integer state,
                                        @Param("type") Integer type,
                                        @Param("mode") Integer mode);


    @Select("SELECT tag.id,tag.tag,tag.type,tag.description FROM tag WHERE id IN(SELECT course_tag.tag_id FROM course_tag WHERE course_id=#{id})")
    List<Tag> getCourseTagById(Integer id);


    @Select("<script>" +
            "SELECT * FROM course WHERE delflag=0 AND id=#{id} AND template=#{tempFlag}" +
            "<if test='state gt 0'>AND state =#{state}</if>"
            +
            "</script>"
    )
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "mode", property = "mode"),
            @Result(column = "course_img", property = "courseImg"),
            @Result(column = "updated_time", property = "updatedTime"),
            @Result(column = "score", property = "score"),
            @Result(column = "state", property = "state"),
            @Result(column = "require_flag", property = "requireFlag"),
            @Result(column = "template", property = "template"),
            @Result(column = "duration", property = "duration"),
            @Result(column = "video", property = "video"),
            @Result(column = "start_time", property = "startTime"),
            @Result(column = "end_time", property = "endTime"),
            @Result(column = "college", property = "collegeInfo", one = @One(select = "com.think.teaching.mapper.CollegeMapper.getInfoById", fetchType = FetchType.EAGER)),
            @Result(column = "id", property = "tags",
                    one = @One(select = "com.think.teaching.mapper.CourseMapper.getCourseTagById", fetchType = FetchType.EAGER)),
            @Result(column = "type", property = "typeInfo",
                    one = @One(select = "com.think.teaching.mapper.CourseTypeMapper.getInfoById", fetchType = FetchType.EAGER)),
            @Result(column = "id", property = "chapters",
                    one = @One(select = "com.think.teaching.mapper.ChapterMapper.getChaptersByCourseId", fetchType = FetchType.EAGER))
    })
    Course getCourseDetails(@Param("id") Integer id,
                            @Param("tempFlag") Integer tempFlag,
                            @Param("state") Integer state);

    @Select("<script>" +
            "SELECT * FROM course WHERE delflag=0 " +
            "<if test='type gt 0'> AND type=#{type}</if>"
            +
            "<if test='state gte 0'> AND state=#{state}</if>"
            +
            "<if test='mode gt 0'> AND mode = #{mode}</if>"
            +
            "<if test='type gt 0'> AND type = #{type}</if>"
            +
            "<if test='tag gt 0'> AND id IN(SELECT course_tag.course_id FROM course_tag WHERE tag_id=#{tag})</if>"
            +
            "<if test='userId gt 0'> AND created_by=#{userId}</if>"
            +
            " AND template=#{tempFlag}"
            +
            "</script>"
    )
    @Result(column = "id", property = "id")
    @Result(column = "type", property = "type")
    @Result(column = "id", property = "teacherInfo", javaType = Map.class, one = @One(select = "com.think.teaching.mapper.CourseMemberMapper.GetFirstMember", fetchType = FetchType.EAGER))
    @Result(column = "type", property = "typeInfo",
            one = @One(select = "com.think.teaching.mapper.CourseTypeMapper.getInfoById", fetchType = FetchType.EAGER))
    IPage<Course> adminSelectCourseWithFilter(IPage<Course> page,
                                              @Param("type") Integer type,
                                              @Param("tag") Integer tag,
                                              @Param("state") Integer state,
                                              @Param("mode") Integer mode,
                                              @Param("tempFlag") Integer tempFlag,
                                              @Param("userId") Integer userId);


    @Select("<script>" +
            "SELECT * FROM course WHERE delflag=0 " +
            "<if test='type gt 0'> AND type=#{type}</if>"
            +
            "<if test='state gte 0'> AND state=#{state}</if>"
            +
            "<if test='mode gt 0'> AND mode = #{mode}</if>"
            +
            "<if test='type gt 0'> AND type = #{type}</if>"
            +
            "<if test='tag gt 0'> AND id IN(SELECT course_tag.course_id FROM course_tag WHERE tag_id=#{tag})</if>"
            +
            "<if test='userId gt 0'> AND created_by=#{userId}</if>"
            +
            " AND template=#{tempFlag}"
            +
            "</script>"
    )
    IPage<Course> adminSelectCourseBaseInfoWithFilter(IPage<Course> page,
                                                      @Param("type") Integer type,
                                                      @Param("tag") Integer tag,
                                                      @Param("state") Integer state,
                                                      @Param("mode") Integer mode,
                                                      @Param("tempFlag") Integer tempFlag,
                                                      @Param("userId") Integer userId);

    @Select("<script>" +
            "SELECT IFNULL(ch.count,0) as count ,course.* FROM course LEFT JOIN (SELECT COUNT(h.course_id) as count,course_id FROM `course_history` h GROUP BY h.course_id) ch ON ch.course_id = course.id WHERE delflag=0 " +
            "<if test='type gt 0'> AND type=#{type}</if>"
            +
            "<if test='mode gt 0'> AND mode = #{mode}</if>"
            +
            "<if test='type gt 0'> AND type = #{type}</if>"
            +
            "<if test='tag gt 0'> AND id IN(SELECT course_tag.course_id FROM course_tag WHERE tag_id=#{tag})</if>"
            +
            " AND template=0 AND state=1 ORDER BY count DESC,created_time"
            +
            "</script>"
    )
    @Result(column = "id", property = "id")
    @Result(column = "id", property = "teacherInfo", javaType = Map.class, one = @One(select = "com.think.teaching.mapper.CourseMemberMapper.GetFirstMember", fetchType = FetchType.EAGER))
    IPage<Map<String, Object>> openSelectCourseWithFilter(IPage<Map<String, Object>> page,
                                                          @Param("type") Integer type,
                                                          @Param("tag") Integer tag,
                                                          @Param("mode") Integer mode);

    @Select("<script>" +
            "SELECT * FROM course WHERE delflag=0 AND id IN(SELECT c.course_id FROM course_user c WHERE c.student_id=#{userId}) "
            +
            "<if test='type gt 0'>AND type=#{type}</if>"
            +
            "<if test='mode gt 0'> AND mode = #{mode}</if>"
            +
            "</script>"
    )
    IPage<Course> getUserSelectedCourseWithFilter(IPage<Course> page,
                                                  @Param("userId") Integer userId, @Param("type") Integer type,
                                                  @Param("mode") Integer mode, @Param("tag") Integer tag);

    @Select("SELECT IFNULL(ch.count,0) as count ,course.* FROM course LEFT JOIN (SELECT COUNT(h.course_id) as count,course_id FROM `course_history` h GROUP BY h.course_id) ch ON ch.course_id = course.id WHERE course.state=1 ORDER BY count DESC,created_time DESC LIMIT #{count};")
    @Result(column = "id", property = "id")
    @Result(column = "id", property = "teacherInfo", javaType = Map.class, one = @One(select = "com.think.teaching.mapper.CourseMemberMapper.GetFirstMember", fetchType = FetchType.EAGER))
    List<Map<String, Object>> getRecommondCourse(Integer count);
}
